package com.compty.paint.domain;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.compty.paint.util.Constants;
import com.compty.security.domain.User;

/**
 * @author zhuqh 展览信息
 */
@Entity
@Table(name = "t_exhibition")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Exhibition implements java.io.Serializable {

	private static final long serialVersionUID = 4762534514720067928L;

	private Long id;

	/************ 主题 ****************/
	private String title;

	/************ 海报 ****************/
	private ImageInfo imageInfo;
	
	/************ 描述（序言） ****************/
	private String description;

	/************ 艺术家 ****************/
	private String artists;

	/************ 发布者，参展单位或个人 ****************/
	private String publisher;

	/************ 上传状态 ****************/
	private UploadState uploadState;

	/************ 审核状态 ****************/
	private AuditState auditState;

	/************ 展览作品集 ****************/
	private List<Painting> paintings = new ArrayList<Painting>();
	
	/************ 开展时间 ****************/
	private Date beginTime;
	
	/************ 展览结束时间 ****************/
	private Date endTime;
	
	/************ 省份 ****************/
	private ProvinceInfo provinceInfo;

	/************ 城市 ****************/
	private CityInfo cityInfo;
	
	/************ 详细地址 ****************/
	private String address;
	
	/************ 上传时间 ****************/
	private Date uploadTime;
	
	/************ 最后修改时间  ****************/
	private Date modifyTime;
	
	/************ 录入人  ****************/
	private User entryPerson;
	
	/************ 作品总件数  ****************/
	private Integer totalCount = 0;
	
	/************ 数量单位  ****************/
	private String unit;
	
	/************ 已售件数  ****************/
	private Integer soldCount;
	
	/************ 展览类型  ****************/
	private String type;  //1表示网上展览，2表示全国展览
	
	@Transient
	private String currentStateFlag = "3";//当前进展状态，预展、进行中，已结束
	
	private String imagepath;//图片路径
	
	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "name", length = 100)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "artists", length = 200)
	public String getArtists() {
		return artists;
	}

	public void setArtists(String artists) {
		this.artists = artists;
	}

	@Column(name = "publisher", length = 100)
	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	 @ManyToMany(cascade =  {CascadeType.REMOVE}
		    , fetch = FetchType.LAZY)
		    @JoinTable(name = "T_exhibition_painting", joinColumns =  {
		        @JoinColumn(name = "exhibition_id")
		    }
		    , inverseJoinColumns =  {
		        @JoinColumn(name = "painting_id")
		    }
    )
	public List<Painting> getPaintings() {
		return paintings;
	}

	public void setPaintings(List<Painting> paintings) {
		this.paintings = paintings;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "uploadstate_id")
	public UploadState getUploadState() {
		return uploadState;
	}

	public void setUploadState(UploadState uploadState) {
		this.uploadState = uploadState;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "auditstate_id")
	public AuditState getAuditState() {
		return auditState;
	}

	public void setAuditState(AuditState auditState) {
		this.auditState = auditState;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="userinfo_id")
	public User getEntryPerson() {
		return entryPerson;
	}

	public void setEntryPerson(User entryPerson) {
		this.entryPerson = entryPerson;
	}

	@Column(name = "description",length=2000)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Column(name = "uploadtime")
	public Date getUploadTime() {
		return uploadTime;
	}

	public void setUploadTime(Date uploadTime) {
		this.uploadTime = uploadTime;
	}

	@Column(name = "modifytime")
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	@Column(name = "begintime")
	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	@Column(name = "endtime")
	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	@Column(name = "total_count")
	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	@Column(name = "sold_count")
	public Integer getSoldCount() {
		return soldCount;
	}

	public void setSoldCount(Integer soldCount) {
		this.soldCount = soldCount;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade =  {CascadeType.REMOVE })
	@JoinColumn(name="image_id")
	public ImageInfo getImageInfo() {
		return imageInfo;
	}

	public void setImageInfo(ImageInfo imageInfo) {
		this.imageInfo = imageInfo;
	}

	@Column(name = "unit",length = 20)
	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="province_id")
	public ProvinceInfo getProvinceInfo() {
		return provinceInfo;
	}

	public void setProvinceInfo(ProvinceInfo provinceInfo) {
		this.provinceInfo = provinceInfo;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="city_id")
	public CityInfo getCityInfo() {
		return cityInfo;
	}

	public void setCityInfo(CityInfo cityInfo) {
		this.cityInfo = cityInfo;
	}

	@Column(name = "address",length = 400)
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Column(name = "type",length = 20)
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Transient
	public String getCurrentStateFlag() {
		Date  currentDate = Calendar.getInstance().getTime();
		if( beginTime != null){
			int flag = currentDate.compareTo(beginTime);
			if(flag > 0){
				if( endTime != null){
					int flag2 = currentDate.compareTo(endTime);
					if( flag2 > 0){
						currentStateFlag = "3";
					}else{
						currentStateFlag = "2";
					}
				}else{
					currentStateFlag = "3";
				}
			}else{
				currentStateFlag = "1";
			}
		}else{
			if(endTime != null){
				int flag2 = currentDate.compareTo(endTime);
				if( flag2 > 0){
					currentStateFlag = "3";
				}else{
					currentStateFlag = "2";
				}
			}else{
				currentStateFlag = "3";
			}
		}
		return currentStateFlag;
	}

	public void setCurrentStateFlag(String currentStateFlag) {
		this.currentStateFlag = currentStateFlag;
	}
	
	@Transient
	public String getImagepath() {
		if( imageInfo!= null ){
			this.imagepath = Constants.IMAGE_SERVER+Constants.ExhibitionFlag+"/"+imageInfo.getId()+"."+imageInfo.getSuffix();
		}
		else{
			this.imagepath = Constants.IMAGE_SERVER ;
		}
		return imagepath;
	}
	
}
